package com.e3.bill.client;

import com.e3.web.core.model.SuperModel;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.Element;

public abstract class BillCardPanel<D extends SuperModel,M extends SuperModel> extends LayoutContainer {

	private BillFormPanel<D> topFormPanel;
	
	private BillCardGridPanel<M> centerGridPanel;
	
	private BillFormPanel<D> bottomFormPanel;

	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		this.setLayout(new FitLayout());
		
		ContentPanel contentPanel = new ContentPanel();
		initContentPanel(contentPanel);

		add(contentPanel);
	}
	
	protected void initContentPanel(ContentPanel contentPanel){
		contentPanel.setLayout(new BorderLayout());
		contentPanel.setHeaderVisible(false);
		
		BorderLayoutData bld = new BorderLayoutData(LayoutRegion.NORTH);
		initTopLayoutData(bld);
		contentPanel.add(getTopFormPanel(),bld);
		
		bld = new BorderLayoutData(LayoutRegion.CENTER);
		initCenterLayoutData(bld);
		contentPanel.add(getCenterGridPanel(),bld);
		
		bld = new BorderLayoutData(LayoutRegion.SOUTH);
		initBottomLayoutData(bld);
		contentPanel.add(getBottomFormPanel(),bld);
	}

	public BillFormPanel<D> getTopFormPanel() {
		if(topFormPanel == null){
			topFormPanel = createTopFormPanel();
		}
		return topFormPanel;
	}

	public BillCardGridPanel<M> getCenterGridPanel() {
		if(centerGridPanel == null){
			centerGridPanel = new BillCardGridPanel<M>(createBillGridPanel());
		}
		return centerGridPanel;
	}

	public BillFormPanel<D> getBottomFormPanel() {
		if(bottomFormPanel == null){
			bottomFormPanel = createBottomFormPanel();
		}
		return bottomFormPanel;
	}
	
	protected void initTopLayoutData(BorderLayoutData bld){
		bld.setSplit(true);
		bld.setSize(100);
	}
	
	protected void initCenterLayoutData(BorderLayoutData bld){
	}
	
	protected void initBottomLayoutData(BorderLayoutData bld){
		bld.setSize((float)0.1);
		bld.setSplit(true);
	}
	
	protected abstract BillFormPanel<D> createTopFormPanel();
	
	protected abstract BillGridPanel<M> createBillGridPanel();
	
	protected abstract BillFormPanel<D> createBottomFormPanel();
	
}
